package leetcode

/*
大数计算器
超过int64位的大数该如何计算呢，我们就不再使用int类型，而是使用string类型，返回也是字符串。
*/

type BigCount struct{}

func NewBigCount() *BigCount {
	return &BigCount{}
}

func (bc *BigCount) Add(number1, number2 string) string {
	res := ""
	resList := []int{}
	a, b, c := 0, 0, 0
	i, j := len(number1), len(number2)
	for i > 0 && j > 0 {
		if i > 0 {
			a = bc.charToInt(number1[i-1])
		} else {
			a = 0
		}
		if j > 0 {
			b = bc.charToInt(number2[j-1])
		} else {
			b = 0
		}
		resList = append(resList, (a+b+c)%10)
		c = (a + b + c) / 10
		i--
		j--
	}
	if c > 0 {
		resList = append(resList, c)
	}
	for i := len(resList) - 1; i >= 0; i-- {
		res += bc.intToStr(resList[i])
	}
	return res
}

func (bc *BigCount) charToInt(ch byte) int {
	return int(ch - '0')
}

func (bc *BigCount) intToStr(i int) string {
	return string(rune(i + '0'))
}
